<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>01 懒加载</title>
    <style>
        .box {
            width: 600px;
        }
        
        img {
            width: 600px;
        }
    </style>
</head>

<body>
    <div class="box">
        <img src="img/loading.gif" data-src="img/嘿嘿嘿.gif" alt="">
        <img src="img/loading.gif" data-src="img/嘿嘿嘿.gif" alt="">
        <img src="img/loading.gif" data-src="img/嘿嘿嘿.gif" alt="">
        <img src="img/loading.gif" data-src="img/小绿帽.gif" alt="">
        <img src="img/loading.gif" data-src="img/小绿帽.gif" alt="">
        <img src="img/loading.gif" data-src="img/小绿帽.gif" alt="">
        <img src="img/loading.gif" data-src="img/蹦瞎卡拉卡.gif" alt="">
        <img src="img/loading.gif" data-src="img/蹦瞎卡拉卡.gif" alt="">
        <img src="img/loading.gif" data-src="img/蹦瞎卡拉卡.gif" alt="">
        <img src="img/loading.gif" data-src="img/蹦瞎卡拉卡.gif" alt="">
        <img src="img/loading.gif" data-src="img/蹦瞎卡拉卡.gif" alt="">
        <img src="img/loading.gif" data-src="img/1.jpg" alt="">
        <img src="img/loading.gif" data-src="img/2.jpg" alt="">
        <img src="img/loading.gif" data-src="img/3.jpg" alt="">
        <img src="img/loading.gif" data-src="img/4.jpg" alt="">
        <img src="img/loading.gif" data-src="img/5.jpg" alt="">
        <img src="img/loading.gif" data-src="img/6.jpg" alt="">
        <img src="img/loading.gif" data-src="img/7.jpg" alt="">
        <img src="img/loading.gif" data-src="img/8.jpg" alt="">
        <img src="img/loading.gif" data-src="img/9.jpg" alt="">
        <img src="img/loading.gif" data-src="img/10.jpg" alt="">
        <img src="img/loading.gif" data-src="img/11.jpg" alt="">
        <img src="img/loading.gif" data-src="img/12.jpg" alt="">
        <img src="img/loading.gif" data-src="img/13.jpg" alt="">
        <img src="img/loading.gif" data-src="img/14.jpg" alt="">
        <img src="img/loading.gif" data-src="img/15.jpg" alt="">
        <img src="img/loading.gif" data-src="img/16.jpg" alt="">
        <img src="img/loading.gif" data-src="img/17.jpg" alt="">
    </div>

    <script>
        var imgs = document.querySelectorAll('.box img');
        window.onscroll = function() {
            var scrollTop = document.documentElement.scrollTop;
            var windowHeight = window.innerHeight;
            for (var i = 0; i < imgs.length; i++) {
                if (scrollTop + windowHeight > imgs[i].offsetTop) {
                    imgs[i].src = imgs[i].getAttribute('data-src');
                }
            }
        }
    </script>
</body>

</html>